package cc.mxtt.common.app;

import java.io.File;
import java.io.IOException;
import java.util.List;

import cc.mxtt.common.bean.PileInfoBean;
import cc.mxtt.common.bean.ProjectBean;
import cc.mxtt.common.core.GlobalKey;
import cc.mxtt.common.utils.ProiInfo;
import cc.mxtt.common.utils.ZipUtil;
import cc.mxtt.mvc.model.Dept;
import cc.mxtt.mvc.model.Pile;
import cc.mxtt.mvc.model.Project;
import cc.mxtt.mvc.model.User;

import com.google.gson.Gson;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;

public class UploadFileTest extends Controller {
	@Clear
	public void index() {
		render("upload.jsp");

	}

	@Clear
	public void upload() {

		// renderText("TEST");

		// getFiles();

		String path_web_root = null;
		// 保存到/WebRoot/files/upload
		StringBuilder web_root = new StringBuilder();
		web_root.append(PathKit.getWebRootPath()).append(File.separator)
				.append("files").append(File.separator).append("upload");
		path_web_root = web_root.toString();

		UploadFile upload = getFile("file", File.separator + "zip", 52428800);
		// UploadFile upload = getFile();

		String name = upload.getFileName();
		String path = upload.getUploadPath();
		
		String info  = getPara("info"); 
		String type  = getPara("type"); // 0 pile 1 project
		String userName = getPara("user");
		
		User user = User.dao.findFirst("select * from sys_user where username =?", userName);
		System.out.println();
		Long deptId = user.getLong("dept_id");
		System.out.println("deptId:"+deptId);
		String sql = "select t.id,t.name,t.description,t.parent_id,t.order_id from sys_dept t where t.id=?";
		Dept dept = Dept.dao.findFirst(sql,deptId);
		Long id = dept.getLong("parent_id");
		System.out.println("parent_id:"+id);
		
		
		Gson json = new Gson();
		if(type.equals("0")){
			
			PileInfoBean pile =json.fromJson(info, PileInfoBean.class);
			Pile s = new Pile();
			s.set("name", pile.getPileName()).set("testLen",pile.getTestLen()).set("project",pile.getProjectName())
			.set("sectionNum", pile.getSectionNum()).set("section", pile.getSection())
			.set("testDate", pile.getTestDate()).set("path", path+File.separator+pile.getPileName()).set("parent_id", id).save();
		}else{
			ProiInfo proInfo = json.fromJson(info, ProiInfo.class);
			
			//判断project是否存在  不存在 未分类  分类关联
			
			ProjectBean pro = proInfo.getPro();
			List<Project> pros = Project.dao.find("select * from sys_proj where name ='"+pro.getName()+"'");
			List<PileInfoBean> piles = proInfo.getPiles();
			if(pros != null && pros.size() > 0){
				for(PileInfoBean pile:piles ){
					Pile s = new Pile();
					s.set("name", pile.getPileName()).set("projectId", pros.get(0).get("id")).set("testLen",pile.getTestLen())
					.set("sectionNum", pile.getSectionNum()).set("section", pile.getSection())
					.set("testDate", pile.getTestDate()).set("path", path+File.separator+pile.getProjectName()+File.separator+pile.getPileName())
					.set("parent_id", id).save();
				}
				
			}else{
				for(PileInfoBean pile:piles ){
					Pile s = new Pile();
					s.set("name", pile.getPileName()).set("testLen",pile.getTestLen()).set("project",pile.getProjectName())
					.set("sectionNum", pile.getSectionNum()).set("section", pile.getSection())
					.set("testDate", pile.getTestDate()).set("path", path+File.separator+pile.getProjectName()+File.separator+pile.getPileName())
					.set("parent_id", id).save();
				}
				
			}
			
		}

		renderText("upload name:" + name + "path:" + path);
		System.out.println("upload name:" + name + "path:" + path);

		// 解压

		try {
			ZipUtil.unzip(path+File.separator+name, "");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
